package com.share.thread;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @desc:
 * @author:caifan
 * @date:2022/3/26
 */
public class SimpleDateFormat01 {
//    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    ThreadLocal<SimpleDateFormat> formatLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        public SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1000));

    public static void main(String[] args) {
        new SimpleDateFormat01().test();
    }

    public void test() {
        while (true) {
            poolExecutor.execute(() -> {
                SimpleDateFormat format = formatLocal.get();
                String dateString = format.format(new Date());
                try {
                    Date parseDate = format.parse(dateString);
                    String dateString2 = format.format(parseDate);
                    System.out.println(dateString.equals(dateString2));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
